package com.kaka.bus.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kaka.bus.R;
import com.kaka.bus.bean.DrainProductBean;
import com.kaka.bus.engine.amount.TopProduct.TopProductActivity;

import java.util.List;

public class PromptAdapter extends RecyclerView.Adapter {
    private List<DrainProductBean> objects;
    public static int NORMAL = 1;
    public static int BLANK = 2;
    private PromptClick mPromptClick;
    private Activity mActivity;

    public PromptAdapter(Activity activity, List<DrainProductBean> objects, PromptClick mPromptClick) {
        this.objects = objects;
        this.mPromptClick = mPromptClick;
        this.mActivity = activity;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if (viewType == NORMAL) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_prompt, null, false);
            return new NormalHolder(view);
        } else {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_prompt_blank, viewGroup, false);
            return new BlankHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {


        if (viewHolder instanceof NormalHolder) {
            Glide.with(mActivity)
                    .load(objects.get(i).getProduct().getProductIcon()).error(R.drawable.icon_kakadef)
                    .into(((NormalHolder) viewHolder).img_icon);
            ((NormalHolder) viewHolder).tv_title.setText(objects.get(i).getProduct().getProductName());
            ((NormalHolder) viewHolder).tv_subhead.setText(objects.get(i).getProduct().getProductDescription());
            ((NormalHolder) viewHolder).tv_money.setText(objects.get(i).getProduct().getRange());
            ((NormalHolder) viewHolder).tv_apply_people.setText(objects.get(i).getProduct().getApplyCount());

            if (objects.get(i).getProduct().getMarks().size() > 0) {

                ((NormalHolder) viewHolder).ll_contain.removeAllViews();
                for (int a = 0; a < objects.get(i).getProduct().getMarks().size(); a++) {
                    // TODO 动态添加布局(java方式)
                    LayoutInflater inflater = mActivity.getLayoutInflater();
                    LinearLayout view = (LinearLayout) inflater
                            .inflate(R.layout.item_lebal, null);
                    TextView textView1 = view.findViewById(R.id.tv_target);
                    textView1.setText(objects.get(i).getProduct().getMarks().get(a).getMarkDesc());
                    ((NormalHolder) viewHolder).ll_contain.addView(view);
                }
            } else {
                ((NormalHolder) viewHolder).ll_contain.setVisibility(View.GONE);
            }
            ((NormalHolder) viewHolder).btn_apply.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPromptClick.itemClickListemer(objects.get(i));
                }
            });
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (objects.size() > 0) {
            return NORMAL;
        }
        return BLANK;
    }


    @Override
    public int getItemCount() {
        if (objects.size() > 0) {
            return objects.size();
        } else {
            return 1;
        }
    }

    class NormalHolder extends BaseViewHolder {
        ImageView img_icon;
        TextView tv_title;
        TextView tv_money;
        TextView tv_apply_people;
        TextView tv_subhead;
        Button btn_apply;
        LinearLayout ll_contain;

        public NormalHolder(@NonNull View itemView) {
            super(itemView);
            img_icon = itemView.findViewById(R.id.img_icon);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_money = itemView.findViewById(R.id.tv_money);
            tv_apply_people = itemView.findViewById(R.id.tv_apply_people);
            tv_subhead = itemView.findViewById(R.id.tv_subhead);
            btn_apply = itemView.findViewById(R.id.btn_apply);
            ll_contain = itemView.findViewById(R.id.ll_contain);
        }
    }

    class BlankHolder extends BaseViewHolder {

        public BlankHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    public interface PromptClick {
        void itemClickListemer(DrainProductBean productBean);
    }
}
